			/* ********************************** *\

				       Glitch Light Tiny
				    A Project by Brent Reamer
				      BrentBXR @Gmail.com
				 http://code.google.com/brentbxr/

   Glitch Light Tiny is an AVR Attiny based LED effect project. Its strictly
   for entertainment and  decoration. The project uses  either two or  three
   independent LED's or one common cathode RGB.

   The effect is  supposed to be  a multi-color  glitching LED's. Adjust and
   play with the delays to suit your preference...

 	 	 	 \* ********************************* */


#include <avr/io.h>
#include <avr/iotn85.h>
#include <avr/interrupt.h>

#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>

#include <util/atomic.h>
#include <util/delay.h>

#include <cdefs.h>


#define PWMLED_PIN PB0            // OC1A pin (for AT90S8515 use PD5, for ATmega8 use PB1)
#define PWMLED_DDR DDRB
#define PWMLED_PORT PORTB

#define SECLED_PIN PB4
#define SECLED_DDR DDRB
#define SECLED_PORT PORTB

#define GFX_DELAY_LONG 100
#define GFX_DELAY_MID 50
#define GFX_DELAY_SML 10
#define GFX_DELAY_XSML 1

void GlitchFx(void)
{
	unsigned int i;
	ATOMIC_BLOCK(ATOMIC_FORCEON) {
		OCR0A = rand();
		_delay_ms(2);
		i = rand();
		if(i>=(RAND_MAX/2))
		{
			if(i>=(RAND_MAX-(RAND_MAX/4)))
			{
				_delay_ms(GFX_DELAY_LONG);
			} else {
				_delay_ms(GFX_DELAY_SML);
			}
		}

		OCR0A = rand();
		i = rand();
		if(i>=(RAND_MAX/2))
		{
			if(i>=(RAND_MAX-(RAND_MAX/4)))
			{
				_delay_ms(GFX_DELAY_MID);
			} else {
				_delay_ms(GFX_DELAY_XSML);
			}
		}

		i = rand();
		if(i>=(RAND_MAX/2))
		{
			SETBIT(SECLED_PORT,SECLED_PIN);
		} else {
			CLEARBIT(SECLED_PORT,SECLED_PIN);
		}

	}
}

void InitTimer0(void)
{
	SETBIT(PWMLED_DDR,PWMLED_PIN);

	TCCR0A =  _BV(WGM00) | _BV(WGM01) | _BV(COM0A1) | _BV(COM0A0);

	TCCR0B = _BV(CS01) | _BV(CS00);
}
/*
void InitTimer1(void)
{
   SETBIT(TCCR1,CS12);
   SETBIT(TCCR1,CS10);

   SETBIT(TIMSK,TOIE1);

   TCNT1=0;
}
*/
int main(void)
{

	SETBIT(SECLED_DDR,SECLED_PIN);
	CLEARBIT(SECLED_PORT,SECLED_PIN);

	InitTimer0();
	//InitTimer1();


	SETBIT(PWMLED_PORT,PWMLED_PIN);
	SETBIT(SECLED_PORT,SECLED_PIN);
	_delay_ms(1000);
	CLEARBIT(SECLED_PORT,SECLED_PIN);
	CLEARBIT(PWMLED_PORT,PWMLED_PIN);
	_delay_ms(1000);
	SETBIT(PWMLED_PORT,PWMLED_PIN);
	SETBIT(SECLED_PORT,SECLED_PIN);
	_delay_ms(640);
	CLEARBIT(SECLED_PORT,SECLED_PIN);
	CLEARBIT(PWMLED_PORT,PWMLED_PIN);
	_delay_ms(640);
	SETBIT(PWMLED_PORT,PWMLED_PIN);
	SETBIT(SECLED_PORT,SECLED_PIN);
	_delay_ms(200);
	CLEARBIT(SECLED_PORT,SECLED_PIN);
	CLEARBIT(PWMLED_PORT,PWMLED_PIN);
	_delay_ms(200);
	SETBIT(PWMLED_PORT,PWMLED_PIN);
	SETBIT(SECLED_PORT,SECLED_PIN);
	_delay_ms(20);
	CLEARBIT(SECLED_PORT,SECLED_PIN);
	CLEARBIT(PWMLED_PORT,PWMLED_PIN);
	_delay_ms(20);
	SETBIT(PWMLED_PORT,PWMLED_PIN);
	SETBIT(SECLED_PORT,SECLED_PIN);
	_delay_ms(20);
	CLEARBIT(SECLED_PORT,SECLED_PIN);
	CLEARBIT(PWMLED_PORT,PWMLED_PIN);
	_delay_ms(20);
	SETBIT(PWMLED_PORT,PWMLED_PIN);
	SETBIT(SECLED_PORT,SECLED_PIN);
	_delay_ms(20);
	CLEARBIT(SECLED_PORT,SECLED_PIN);
	CLEARBIT(PWMLED_PORT,PWMLED_PIN);
	_delay_ms(20);
	SETBIT(PWMLED_PORT,PWMLED_PIN);
	SETBIT(SECLED_PORT,SECLED_PIN);
	_delay_ms(20);
	CLEARBIT(SECLED_PORT,SECLED_PIN);
	CLEARBIT(PWMLED_PORT,PWMLED_PIN);
	_delay_ms(20);
	SETBIT(PWMLED_PORT,PWMLED_PIN);
	SETBIT(SECLED_PORT,SECLED_PIN);
	_delay_ms(20);
	CLEARBIT(SECLED_PORT,SECLED_PIN);
	CLEARBIT(PWMLED_PORT,PWMLED_PIN);
	_delay_ms(20);


	CLEARBIT(SECLED_PORT,SECLED_PIN);
	CLEARBIT(PWMLED_PORT,PWMLED_PIN);
	_delay_ms(1000);

    for(;;)
    {
    	GlitchFx();
    }

    return main();
}




